﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using GreatB2C.Utility;
using GreatB2C.Business;

namespace GreatB2C.Web.Manage
{
    public partial class Index : Inherit.ManagePage
    {
        /// <summary>
        /// 换行符
        /// </summary>
        const string newline = "\r\n";

        protected void Page_Load( object sender , EventArgs e )
        {
            // 权限验证

            if ( !IsPostBack )
            {
                LoadMenu( );
            }
        }

        public void LoadMenu( )
        {
            StringBuilder sbSubMenu = new StringBuilder( );
            StringBuilder sbNavi = new StringBuilder( );
            Navigator navi = new Navigator( );

            sbNavi.Append( "<ul id=\"ChannelMenuItems\">" + newline );

            DataTable dtNavi = navi.ListNavigator( );
            DataView dvNavi = dtNavi.DefaultView;
            dvNavi.RowFilter = "am_ParentID='" + DataDefine.ModuleRoot + "'";
            dvNavi.Sort = "am_OrderID asc";
            if ( dvNavi.Count > 0 )
            {
                foreach ( DataRowView drv in dvNavi )
                {
                    if ( drv[ "am_ClassID" ].ToString( ) == "001000000000" )
                    {
                        sbNavi.Append( "<li id=\"navi_" + drv[ "am_ClassID" ].ToString( ) + "\" onclick=\"ShowHideLayer('ChannelMenu_MenuMyDeskTop')\">" + newline
                                     + "<a href=\"javascript:\" id=\"AChannelMenu_MenuMyDeskTop\">" + newline
                                     + "<span id=\"SpanChannelMenu_MenuMyDeskTop\" >" + drv[ "am_Name" ].ToString( ) + "</span>" + newline );

                        sbNavi.Append( "<span id=\"Menu" + drv[ "am_ClassID" ].ToString( ) + "_Triangle\" style=\"background-image: url(/App_Themes/AdminDefaultTheme/Images/digital_side.gif);\">" + newline
                                     + "<img border=\"0\" src=\"/App_Themes/AdminDefaultTheme/Images/TopQuick01.gif\" /></span>" + newline );
                    }
                    else
                    {
                        sbNavi.Append( "<li id=\"navi_" + drv[ "am_ClassID" ].ToString( ) + "\" onclick=\"ShowHideLayer('ChannelMenu_" + drv[ "am_ClassID" ].ToString( ) + "')\">" + newline
                                     + "<a href=\"javascript:\" id=\"AChannelMenu_" + drv[ "am_ClassID" ].ToString( ) + "\">" + newline
                                     + "<span id=\"SpanChannelMenu_" + drv[ "am_ClassID" ].ToString( ) + "\" >" + drv[ "am_Name" ].ToString( ) + "</span>" + newline );
                    }

                    DataView dvNavi1 = dtNavi.DefaultView;
                    dvNavi1.RowFilter = "am_ParentID='" + drv[ "am_ClassID" ].ToString( ) + "'";
                    dvNavi1.Sort = "am_OrderID asc";
                    if ( dvNavi1.Count > 0 )
                    {
                        if ( drv[ "am_ClassID" ].ToString( ) == "001000000000" )
                        {
                            sbSubMenu.Append( "<div id=\"ChannelMenu_MenuMyDeskTop\" style=\"width: 100%;\">" + newline );
                            sbSubMenu.Append( "<ul>" + newline );
                            sbSubMenu.Append( "<li>管理员：<span class=\"AdminName\"><strong>Admin</strong></span>，欢迎您！&nbsp;</li>" + newline
                                            + "<li><a id=\"LinkButton1\" href=\"Logout.aspx\"><span class=\"SignOut\">安全退出</span></a></li>" + newline );
                        }
                        else
                        {
                            sbSubMenu.Append( "<div id=\"ChannelMenu_" + drv[ "am_ClassID" ].ToString( ) + "\" style=\"width: 100%;display:none;\">" + newline );
                            sbSubMenu.Append( "<ul>" + newline );
                        }
                        GetSubMenu( drv[ "am_ClassID" ].ToString( ) , dtNavi , ref sbSubMenu , false );
                        sbSubMenu.Append( "</ul>" + newline );
                        sbSubMenu.Append( "</div>" + newline );
                    }

                    sbNavi.Append( "</a></li>" + newline );
                }
            }

            sbNavi.Append( "</ul>" + newline );
            sbNavi.Append( "<div id=\"SubMenu\">" + newline );
            sbNavi.Append( sbSubMenu.ToString( ) );
            sbNavi.Append( "</div>" + newline );

            litNavi.Text = sbNavi.ToString( );
        }

        public void GetSubMenu( string parentID , DataTable dtSource , ref StringBuilder sbSubMenu , bool isSub )
        {
            DataView dvNavi = dtSource.DefaultView;
            dvNavi.RowFilter = "am_ParentID='" + parentID + "'";
            dvNavi.Sort = "am_OrderID asc";
            if ( dvNavi.Count > 0 )
            {
                foreach ( DataRowView drv in dvNavi )
                {
                    DataView dvNavi1 = dtSource.DefaultView;
                    dvNavi1.RowFilter = "am_ParentID='" + drv[ "am_ClassID" ].ToString( ) + "'";
                    dvNavi1.Sort = "am_OrderID asc";
                    if ( dvNavi.Count > 0 )
                    {
                        sbSubMenu.Append( "<li onmouseover=\"show('" + drv[ "am_ClassID" ].ToString( ) + "');hideOthers('" + drv[ "am_ClassID" ].ToString( ) + "');\" onmouseout=\"hide('" + drv[ "am_ClassID" ].ToString( ) + "')\">" + newline
                                        + "<a href=\"javascript:ShowMain('" + drv[ "am_FilePath" ].ToString( ) + "','" + drv[ "mainURL" ].ToString( ) + "')\" onclick=\"\">" + drv[ "am_Name" ].ToString( ) + "</a>" + newline
                                        + "<div id=\"PopMenu_" + drv[ "am_ClassID" ].ToString( ) + "\" onmouseover=\"show('" + drv[ "am_ClassID" ].ToString( ) + "')\" onmouseout=\"hide('" + drv[ "am_ClassID" ].ToString( ) + "')\" class=\"SubMenuDiv\" onclick=\"hide('" + drv[ "am_ClassID" ].ToString( ) + "')\">" + newline
                                        + "<dl>" + newline
                                        + "<dd>" + newline );

                        GetSubMenu( drv[ "am_ClassID" ].ToString( ) , dtSource , ref sbSubMenu , true );

                        sbSubMenu.Append( "</dd>" + newline
                                        + "</dl>" + newline
                                        + "<iframe id=\"iframe_" + drv[ "am_ClassID" ].ToString( ) + "\" width=\"100%\" frameborder=0 height=\"300px\" style=\"position:absolute; top:0px; z-index:-1; border-style:none;\"></iframe>" + newline
                                        + "</div>" + newline
                                        + "</li>" + newline );
                    }
                    else
                    {
                        if ( isSub )
                            sbSubMenu.Append( "<span><a href=\"javascript:ShowMain('" + drv[ "am_FilePath" ].ToString( ) + "','" + drv[ "mainURL" ].ToString( ) + "')\">" + drv[ "am_Name" ].ToString( ) + "</a></span>" + newline );
                        else
                            sbSubMenu.Append( "<li><a href=\"javascript:ShowMain('" + drv[ "am_FilePath" ].ToString( ) + "','" + drv[ "mainURL" ].ToString( ) + "')\">" + drv[ "am_Name" ].ToString( ) + "</a></li>" + newline );
                    }
                }
            }
        }

    }
}
